package com.itheima.wms.service.impl;

import com.itheima.wms.mapper.AreaMapper;
import com.itheima.wms.mapper.LocationMapper;
import com.itheima.wms.mapper.WareHouseMapper;
import com.itheima.wms.pojo.Location;
import com.itheima.wms.pojo.LocationDto;
import com.itheima.wms.pojo.LocationPageQueryVo;
import com.itheima.wms.pojo.LocationVO;
import com.itheima.wms.service.LocationService;
import com.itheima.wms.utils.PageBean;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Service
public class LocationServiceImpl implements LocationService {

    private final LocationMapper locationMapper;

    private final AreaMapper areaMapper;

    private final WareHouseMapper wareHouseMapper;

    @Override
    public PageBean<Location> queryLocationPageByCondition(LocationPageQueryVo locationPageQueryVo) {
        //每页大小
        Integer pageSize = locationPageQueryVo.getSize();
        //当前页
        Integer page = locationPageQueryVo.getCurrent();
        Long total = locationMapper.queryLocationCount();
        Integer start = (page - 1) * pageSize;

        List<Location> locations = locationMapper.queryLocationPageByCondition(start, pageSize, locationPageQueryVo);

        PageBean<Location> pageBean = new PageBean<>(locations, total, pageSize);
        return pageBean;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertLocation(Location location) {
        Long areaId = location.getAreaId();
        locationMapper.addLocation(location);
        areaMapper.plusIncludeNum(areaId);
    }

    @Override
    public Location queryLocationById(Long id) {
        Location location = locationMapper.queryLocationById(id);
        return location;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        // TODO: 2024/5/31 删除库位时 要将库区相关数量减一
        Location location = locationMapper.queryLocationById(id);
        Long areaId = location.getAreaId();
        areaMapper.minusIncludeNum(areaId);
        locationMapper.delete(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Location location) {
        if (location.getWarehouseId() == null) {
            //禁用
            locationMapper.updateOnlyStatus(location);
        } else {
            //修改
            // TODO: 2024/5/31   查询之前的库区 和 之后的库区进行对比
            Long id = location.getId();
            //修改之前的location信息
            Location oldlocation = locationMapper.queryLocationById(id);
            Long oldAreaId = oldlocation.getAreaId();
            Long newWarehouseId = location.getWarehouseId();
            Long oldWarehouseId = oldlocation.getWarehouseId();
            Long newAreaId = location.getAreaId();
            if ((oldWarehouseId == newWarehouseId) && (oldAreaId == newAreaId)) {
                //未修改areaid和warehouseid
            } else {
                areaMapper.minusIncludeNum(oldAreaId);
                areaMapper.plusIncludeNum(newAreaId);
            }
            locationMapper.update(location);
        }
    }

    @Override
    public List<LocationVO> locationDetail(LocationDto locationDto) {
        List<LocationVO> locationVOList = locationMapper.locationDetail(locationDto);
        return locationVOList;
    }


}
